<?php

class ORM_Finder{
    
    /**
     *
     * @param type $model
     * @param type $conditions
     * @param type $fields
     * @param string $order
     * @param string $limit
     * @return model 
     */
    static function find($model, $conditions = null, $fields = '*', $order = null, $limit = null) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        if (!empty($conditions)) {
            $where = "WHERE";
            if (is_array($conditions)) {
                $where  = array();
                foreach ($conditions as $field => $value) {
                    $where [] = " $field = ?";
                    $values[] = $value;
                }
                $where  = ' WHERE ' . implode(' and ', $where);
            } else {
                $where .= ' '.$conditions;
                $values = array();
            }
        } else {
            $where = '';
            $values = array();
        }
        
        is_array($fields) ? $fields = join(',', $fields) : $fields;
        
        if (!empty($order)) {
            $order = 'ORDER BY '.$order;
        }
        if (!empty($limit)) {
            $limit = 'LIMIT '.$limit;
        }

        $table = call_user_func(array($model, 'getTableName'), $conditions);
        $sql     = "SELECT $fields FROM {#prefix#}".$table." $where $order $limit";
        $rs = $conn->query($sql, $values);
        //$rs->setFetchMode(PDO::FETCH_OBJ);
        $return = array();
        while ($data = $rs->fetch()) {
            $m = new $model;
            foreach ($data as $key => $d) {
                call_user_func(array($m, 'set'.$key), $d);
            }
            $m->setNew(false);
            $return[] = $m;
        }
        return $return;
    }
    
    static function findBySql($model, $conditions = null,$values=array()) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $rs = $conn->query($conditions, $values);
        //$rs->setFetchMode(PDO::FETCH_OBJ);
        $return = array();
        while ($data = $rs->fetch()) {
            $return[] = $data;
        }
        return $return;
    }
    
    /**
     * 检索一条数据
     *
     * @param string $model
     * @param array $conditions
     * @param array $fields
     * @param string $order
     * @return unknown
     */
    static function findOne($model, $conditions = null, $fields = '*', $order = null, $limit=1) {
        $r = self::find($model, $conditions, $fields, $order, $limit);
        return isset($r[0])?$r[0]:null;
    }
    
    static function findById($model, $id) {
        $r = self::find($model, array('id' => $id), '*', null, 1);
        return $r[0];
    }
    
    static function findCount($model, $conditions = null) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        
        if (!empty($conditions)) {
            $where = "WHERE";
            if (is_array($conditions)) {
                $where  = array();
                foreach ($conditions as $field => $value) {
                    $where [] = " $field = ?";
                    $values[] = $value;
                }
                $where  = ' WHERE ' . implode(' and ', $where);
            } else {
                $where .= ' '.$conditions;
                $values = array();
            }
        } else {
            $where = '';
            $values = array();
        }
        
        $table = call_user_func(array($model, 'getTableName'), $conditions);
        $sql    = "SELECT count(*) FROM {#prefix#}".$table." $where";
        $rs = $conn->query($sql, $values);
        $return = $rs->fetchColumn();
        if(!$return){
            return 0;
        }
        return $return;
    }
    
    /**
     *
     * @param type $model
     * @param type $conditions
     * @param type $filed
     * @param type $append
     * @return type 
     */
    static function findSum($model, $conditions = null, $filed='id', $append=null) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        
        if (!empty($conditions)) {
            $where = "WHERE";
            if (is_array($conditions)) {
                $where  = array();
                foreach ($conditions as $field => $value) {
                    $where [] = " $field = ?";
                    $values[] = $value;
                }
                $where  = ' WHERE ' . implode(' and ', $where);
            } else {
                $where .= ' '.$conditions;
                $values = array();
            }
        } else {
            $where = '';
            $values = array();
        }
        
        $table = call_user_func(array($model, 'getTableName'), $conditions);
        $sql    = "SELECT sum($filed) as total FROM {#prefix#}".$table." $where ".$append;
        $rs = $conn->query($sql, $values);
        $return = $rs->fetch();
        if(!$return){
            return 0;
        }
        return $return['total'];
    }
    
    /**
     *
     * @param type $sql
     * @param type $values
     * @param type $model
     * @return type 
     */
    static function execSql($sql,$values=array(), $model){
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $table = call_user_func(array($model, 'getTableName'), null);
        return $conn->query($sql, $values)->fetchColumn();
    }
}
